package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.result.ResultBean;
import com.dashu.core.dto.ImgRecordDTO;
import com.dashu.core.service.DocumentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;


/**
 * @Author xys
 * @Date 2020/11/17 15:21
 */

@RequestMapping("dashu/document")
@RestController
@Slf4j
@Api(value = "dashu/document", tags = {"文件处理功能"}, description = "后台")
public class
DocumentController {


    @Autowired
    private DocumentService documentService;

    @PostMapping("/imgRecord")
    @ApiOperation(value = "图片保存", notes = "图片保存")
    public ResultBean<ImgRecordDTO> imgRecord(@RequestParam(value ="pic") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        InputStream instream = file.getInputStream();
        if (instream != null) {
            return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "图片保存", documentService.imgRecord(instream,fileName));
        }
        return new ResultBean<>(ReplRyCode.DATA_NOT_IMG.getCode(), ReplRyCode.DATA_NOT_IMG.getMessage(), null);
    }

}
